from selenium.webdriver.firefox.webdriver import WebDriver

from locators import AccountListLocators
from pages.base import BasePage
from pages.sign_in.AuthenticationPage import AuthenticationPage
from pages.sign_in.AuthorizationPage import AuthorizationPage


class AccountListPage(BasePage):
    URL = "https://passport.yandex.ru/auth/list"

    def __init__(self, driver: WebDriver):
        super().__init__(driver)

    def logout(self):
        self._open_context_menu()
        logout_bt = self.find_element(AccountListLocators.LOGOUT_BT_LOCATOR)
        logout_bt.click()

        self.wait_new_page(AuthorizationPage.URL)
        return AuthenticationPage(self.driver)

    def logout_with_list_flushing(self):
        self._open_context_menu()
        logout_bt = self.find_element(AccountListLocators.LOGOUT_WITH_FLUSH_BT_LOCATOR)
        logout_bt.click()

        self.wait_new_page(AuthenticationPage.URl_BY_BACK_BT)
        return AuthorizationPage(self.driver)

    def _open_context_menu(self):
        context_bt = self.find_element(AccountListLocators.CONTEXT_BT_LOCATOR)
        context_bt.click()